﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ACP.Business;
using ACP.DTO;

namespace ACP.Web.UI.admin
{
    public partial class participants : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) { bindUsers("");  }
        }


        public void bindUsers(string whereClause)
        {
            try
            {
                var users = UsersLogic.GetAllUsers();

                int condCount = 0;
                string sWhere = "";

                //foreach (var userEpId in userEpIds)
                //{
                //    condCount++;
                //    sWhere += ("ep_id ==" + userEpId);
                //    if (condCount < userEpIds.Count())
                //        sWhere += " OR ";
                //}

                //for Searching User
                if (whereClause != "") { sWhere = whereClause; }

                //-----------------| Sort Expression start |---------------
                if (gvUsers.Attributes["sortOrder"] == null) gvUsers.Attributes["sortOrder"] = "DESC";
                if (gvUsers.Attributes["SortColumn"] == null) gvUsers.Attributes["SortColumn"] = "0";
                if (gvUsers.Attributes["sortExp"] == null)
                {
                    gvUsers.Attributes["sortExp"] = "registrationdate";
                }
                string StrSortExpression = " " + gvUsers.Attributes["sortExp"] + " " + gvUsers.Attributes["sortOrder"];

                //-----------------| Sort Expression End |----------

                //var query = db.vw_GetUserDetails.Where(sWhere);

                //dtUsers = CommonLogic.ToDataTable(db, query);
                //DataView dvUsers = dtUsers.DefaultView;
               // dvUsers.Sort = StrSortExpression;
                if (users.Count() > 0)
                {
                    gvUsers.DataSource = users;
                    gvUsers.DataBind();
                }
            }
            catch (Exception ex)
            {
               throw ex ;
            }
            finally {  }
           

        }

        protected void gvUsers_RowEditing(object sender, GridViewEditEventArgs e)
        {
            //PgateGitsDataContext db = new PgateGitsDataContext();
            //e.Cancel = true;
            //try
            //{
            //    ltrlErr1.Text = string.Empty;
            //    int id = Convert.ToInt32(gvUsers.DataKeys[e.NewEditIndex].Value.ToString());

            //    hdn.Value = id.ToString();
            //    var EditUser = (from User in db.vw_GetUserDetails where User.gu_id == id select User).First();
            //    lblFName.Text = EditUser.EP_FIRST_NAME;
            //    lblLName.Text = EditUser.EP_LAST_NAME;
            //    ddlUsers.SelectedValue = EditUser.gu_ep_id.ToString();
            //    ddlRoles.SelectedValue = EditUser.gu_gr_id.ToString();
            //    //lblEmail.Text = EditUser.EP_EMAIL;
            //    //lblPassword.Text = EditUser.EP_PASSWORD;
            //    ddlLocation.SelectedValue = EditUser.gu_location;
            //    chkActive.Checked = (EditUser.gu_status == 'Y') ? true : false;

            //    //for Image Showing Purpose
            //    string actuvalPath = "", webpath = "";
            //    webpath = ConfigurationManager.AppSettings["WebPath"].ToString();
            //    if (EditUser.gu_img != "")
            //    {
            //        actuvalPath = ConfigurationManager.AppSettings["physicalpath"].ToString() + "userImages\\" + EditUser.gu_img;

            //        if (File.Exists(actuvalPath))
            //        {
            //            img1.Text = string.Empty;
            //            img1.Text = "<a href='#' class='thumbnail'>" + EditUser.gu_img + "<span><img src='" + webpath + "/userImages/" + EditUser.gu_img + "' alt='" + EditUser.EP_FIRST_NAME + " " + EditUser.EP_LAST_NAME + "' height='130' width='130'/></span></a>";
            //        }
            //    }
            //    else
            //    {
            //        img1.Text = "<a href='#' class='thumbnail'>No Image<span><img src='" + webpath + "/userImages/no_photo.jpg' alt='No Image' height='130' width='130'/></span></a>";

            //    }
            //    trNames.Attributes.Add("style", "display:block");
            //    //trLogins.Attributes.Add("style", "display:block");
            //    trActive.Attributes.Add("style", "display:block");
            //    MpeAddEdit.Show();
            //}
            //catch
            //{
            //    ltrl.Text = string.Format(GlobalConstants.FormatErrorMessage, Error);
            //    return;
            //}
            //finally { db.Dispose(); }
        }

        // This is for Sorting Functionality
        protected void gvUsers_Sorting(object sender, GridViewSortEventArgs e)
        {
            string StrExp = e.SortExpression;
            if (StrExp.Equals(gvUsers.Attributes["sortExp"]))
            {
                if (!(gvUsers.Attributes["sortOrder"] == null))
                {
                    if (gvUsers.Attributes["sortOrder"].StartsWith("ASC"))
                    {
                        gvUsers.Attributes["sortOrder"] = "DESC";
                    }
                    else if (gvUsers.Attributes["sortOrder"].StartsWith("DESC"))
                    {
                        gvUsers.Attributes["sortOrder"] = "ASC";
                    }
                }
                else
                {
                    gvUsers.Attributes["sortOrder"] = "ASC";
                }
            }
            gvUsers.Attributes["sortExp"] = e.SortExpression;
            bindUsers("");
        }

        protected void DeleteUser(object sender, EventArgs e)
        {
            //PgateGitsDataContext db = new PgateGitsDataContext();
            //try
            //{
            //    int id = Convert.ToInt32((sender as LinkButton).Attributes["key"].ToString());
            //    var deleteUser = db.gitsUsers.Where(u => u.gu_id == id).SingleOrDefault();
            //    //db.gitsUsers.DeleteOnSubmit(deleteUser);
            //    //for deleting file
            //    //CommonLogic.deleteFile(ConfigurationManager.AppSettings["physicalpath"] + "userImages\\" + deleteUser.gu_img);
            //    deleteUser.gu_status = 'D';
            //    db.SubmitChanges();
            //    bindUsers("");
            //    ltrl.Text = string.Format(GlobalConstants.FormatSuccessMessage, "User Deleted Successfully");
            //}
            //catch
            //{
            //    ltrl.Text = string.Format(GlobalConstants.FormatErrorMessage, Error);
            //    return;
            //}
            //finally { db.Dispose(); }
        }

        protected void gvUsers_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //if (e.Row.RowType == DataControlRowType.DataRow)
            //{
            //    PgateGitsDataContext db = new PgateGitsDataContext();
            //    DataRowView dr = (DataRowView)e.Row.DataItem;
            //    Label lblRoleName = (Label)e.Row.FindControl("lblRoleName");
            //    lblRoleName.Text = db.gitsRoles.Where(r => r.gr_id == Convert.ToInt32(dr["gu_gr_id"])).Select(r => r.gr_desc).SingleOrDefault();
            //}
        }

        protected void gvUsers_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvUsers.PageIndex = e.NewPageIndex;
            bindUsers("");
            //gvUsers.DataBind();
        }

       
    }
}